#include "EngineInstance.h"


EngineInstance::~EngineInstance()
{

}

bool EngineInstance::Initialize(HINSTANCE instance)
{
	// Check to make sure we didn't initialize already
	if(_IsInitialized)
		return false;

	// First initialize the settings since everyone needs it
	_Settings.Initialize();

	// Create the window
	_AppWindow.Initialize(instance, &_Messenger, &_Settings);


	_IsRunning = true;

	_Timer.StartTimer();
	return true;
}

/// Purpose:	Updates the application, gets the elapsed time, and processes windows msgs
bool EngineInstance::HeartBeat()
{
	if(!_IsRunning)
		return false;
	if(GetAsyncKeyState(VK_ESCAPE))
		_Messenger.AddMessage(MT_CLOSE);


	// Update any major system components...
	_AppWindow.Update();			// Update the window to process messages
	_Timer.Update();				// Update the global timer to get our elapsed time
	_Messenger.ProcessMessages();	// Process internal system messages
	///////////////////////////////////////////////


	// Update the game and any managers here...


	//////////////////////////////////////////////


	// Do the rendering last...

	///////////////////////////////////////////////

	Inform("FPS: %d", _Timer.GetFPS());
	float sleepTime = _Timer.GetElapsedTime();
	if(sleepTime < 0.016f)
	{
		Sleep((0.016f-sleepTime)*1000.0f);
	}
	return true;
}

/// Purpose:	Shuts down the engine and uninitializes any major components that are running.
///				Returns true on a successful shutdown.
bool EngineInstance::Shutdown()
{
	// Destroy the window
	_AppWindow.Shutdown();


	// Lastly, reset the settings
	_Settings.ResetToDefault();

	return true;
}

/// Purpose:	Handles messages processed by the system message component
void EngineInstance::HandleMessage(int Message)
{
	switch(Message)
	{
	case MT_CLOSE:
		{
			_IsRunning = false;
			Shutdown();
			break;
		}
	case MT_SHUTDOWN:
		{

			break;
		}
	case MT_RESIZE:
		{

			break;
		}
	case MT_PAUSE:
		{

			break;
		}
	case MT_UNPAUSE:
		{

			break;
		}
	default:
		break;
	};
}


